﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TidsRegWEB
{
    public partial class AdminOpretProjekt : System.Web.UI.Page
    {
        TidsRegSrv.TidsRegServiceClient tid = new TidsRegSrv.TidsRegServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loggedIn"] == "" || Session["Admin"] != "true")
            {
                Response.Redirect("/");
            }
            DropDownList ddl = new DropDownList();
            ddl.ID = "ddlChkList";
            ListItem lstItem = new ListItem();
            ddl.Items.Insert(0, lstItem);
            ddl.Width = new Unit(155);
            ddl.AutoPostBack = true;
            ddl.Attributes.Add("onmousedown", "showdivonClick()");
            CheckBoxList chkBxLst = new CheckBoxList();
            chkBxLst.ID = "chkLstItem";
            chkBxLst.Attributes.Add("onmouseover", "showdiv()");
            DataTable dtListItem = GetListItem();
            int rowNo = dtListItem.Rows.Count;
            string lstValue = string.Empty;
            string lstID = string.Empty;
            for (int i = 0; i < rowNo - 1; i++)
            {
                lstValue = dtListItem.Rows[i]["Value"].ToString();
                lstID = dtListItem.Rows[i]["ID"].ToString();
                lstItem = new ListItem("<a href=\"javascript:void(0)\" id=\"alst\" style=\"text-decoration:none;color:Black; \" onclick=\"getSelectedItem(' " + lstValue + "','" + i + "','" + lstID + "','anchor');\">" + lstValue + "</a>", dtListItem.Rows[i]["ID"].ToString());
                lstItem.Attributes.Add("onclick", "getSelectedItem('" + lstValue + "','" + i + "','" + lstID + "','listItem');");
                chkBxLst.Items.Add(lstItem);
            }
            System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
            div.ID = "divChkList";
            div.Controls.Add(chkBxLst);
            div.Style.Add("border", "black 1px solid");
            div.Style.Add("width", "160px");
            div.Style.Add("height", "180px");
            div.Style.Add("overflow", "AUTO");
            div.Style.Add("display", "none");
            phDDLCHK.Controls.Add(ddl);
            phDDLCHK.Controls.Add(div);
        }

        protected void BtnOpretProjekt_Click(object sender, EventArgs e)
        {
            string navn = TxtOpretProjektNavn.Text;
            string beskrivelse = TxtBeskrivelse.Text;

            tid.OpretProjekt();
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            string strSelectedItem = string.Empty;
            CheckBoxList chk = (CheckBoxList)phDDLCHK.FindControl("chkLstItem");
            DropDownList ddl = (DropDownList)Page.FindControl("ddlChkList");
            for (int i = 0; i < chk.Items.Count; i++)
            {
                if (chk.Items[i].Selected)
                {
                    if (strSelectedItem.Length == 0)
                    {
                        strSelectedItem = chk.Items[i].Value.ToString();
                    }
                    else
                    {
                        strSelectedItem = strSelectedItem + "," + chk.Items[i].Value.ToString();
                    }
                }
            }
            ddl.Items.Clear();
            ddl.Items.Add(new ListItem(hidList.Value));
            lblSelectedItem.Text = strSelectedItem;
        }

        public DataTable GetListItem()
        {
            string[] fisk = tid.HentAlleMedarbejder();
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Value", typeof(string));
            for (int i = 0; i < fisk.Count(); i++)
            {
                string[] elefant = fisk[i].Split(';');
                table.Rows.Add(elefant[1], elefant[0]);
            }
            return table;
        }
    }
}
